<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/XGame_template.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>XGame</title>
<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="head" -->

<!-- InstanceEndEditable -->
<link type="text/css" rel="stylesheet" href="CSS/css.css" />
</head>

<body>
<?php
  include('lock.php'); 
?>
<div id="container">

<div id="wrapper">

<div id="banner">
<!-- InstanceBeginEditable name="TopLink" -->
<?php

if(!empty($_SESSION['login_user'])){
	$login = $_SESSION['login_user'];
	$q = "SELECT user_name FROM user where user_ID = '$login'";
	$rst=mysql_query($q);
	$data_fetch=mysql_fetch_array($rst);
?>
<ul class="topLink">
<li> Welcome, <a href="UserProfile.php"> <?php echo $data_fetch['user_name']; ?> </a> </li>
<li> <a href="Wishlist.php"> Wishlist </a> &nbsp; | &nbsp;<a href="userShoppingCart.php"> Cart </a> </li>
<li> <a href="Logout.php"> Logout </a> </li>
</ul>
<?php
	}
	else{
?>
<div class="welcome">
<p style="float:right; padding-right:10px"> Welcome: &nbsp; (
<a href="Register.php"> LOGIN</a> |
<a href="Register.php"> REGISTER </a>) </p>
</div>
<?php
	}
?>
<!-- InstanceEndEditable -->

<img src="Assets/XGame.png" alt="XGame" />
</div>

<div id="menu">
<ul class="nav">
<form method="post" action="search_result.php">
<li> <span> <input type="text" placeholder="Search Keyword" name="search" />
&nbsp; <input type="submit" name="Search" value="Search" /> </span>
</li>
</form>
<li> <a href="Home.php"> Home </a> </li>
<li> <a href="ListOfNews.php"> News </a> </li>
<li> <a href="Games.php"> Games </a> </li>
<li> <a href="FAQ.php"> FAQ </a> </li>
<li> <a href="ContactUs.php"> Contact Us </a> </li>
</ul>

</div>

<div id="main">
<!-- InstanceBeginEditable name="Main" -->
<div class="verticalNav">
<h3> User Account </h3>
<ul>
<li> <a href="Wishlist.php"> Your Wishlist </a> </li>
<li> <a href="userShoppingCart.php"> Shopping Cart </a> </li>
<li> <a href="userCheckOutList.php"> Checkout List </a> </li>
<li> <a href="History.php"> History </a> </li>
</ul>
</div>

<div id="iframe">
<table border="0" class="table" >
<tr align="center">
<th class="tableHeader"> Product ID</th>
<th class="tableHeader"> Product Name </th>
<th class="tableHeader"> Quantity </th>
<th class="tableHeader">&nbsp;  </th>
<th class="tableHeader"> Unit Price </th>
<th class="tableHeader">&nbsp;  </th>
<th class="tableHeader"> Sub-Total Price </th>
</tr>

<?php
   global $total;
   global $order_date;
   global $location_id;
   global $location_name;
   
   if(isset($_POST['total']))
   {
	   $total=$_POST['total'];
	   /*$ord=$_POST['order_date'];
	   $ord = explode(" ", $order_date);
	   $order_date = $ord[0];*/
	   $order_date=$_POST['order_date'];
	   $location_id=$_POST['location_id'];
	   $location_name=$_POST['location_name'];
	   $displayFromDB = false;
   }
   else $displayFromDB = true;
  //store into order table
  $sql="INSERT INTO `webtech`.`order` (`order_ID`, `order_total_price`, `order_date`, `order_status`, `location_ID`, `user_ID`) VALUES (NULL, '$total', '$order_date', 'Pending', '$location_id', '$login_session')";
  $q3=mysql_query($sql);
  //-- end of storing    

  //store into order_game table      
  $query_count=mysql_query("SELECT demo_ID, COUNT(demo_ID) FROM cart WHERE user_ID='$login_session' GROUP BY demo_ID");

  while($count_demo=mysql_fetch_array($query_count))
  {
  
    $num_demo_id=$count_demo['COUNT(demo_ID)'];
    $demo_id=$count_demo['demo_ID'];

    $ql=mysql_query("SELECT * FROM demo WHERE demo_ID='$demo_id'");
    $data_fetch=mysql_fetch_array($ql);
  
    $demo_name=$data_fetch['demo_name'];
    $demo_price=number_format($data_fetch['demo_price'],2);
    
    $sql="SELECT `order_ID` FROM `order` WHERE user_ID ='$login_session' AND order_date='$order_date'";
    $q2=mysql_query($sql);
   
    $data_fetch2=mysql_fetch_array($q2);
      
    $order_id=$data_fetch2['order_ID']; 
    $get_demo_query=mysql_query("SELECT demo_ID FROM cart WHERE user_ID='$login_session' AND demo_ID='$demo_id'");
    
    while($fetch_demo_id=mysql_fetch_array($get_demo_query))
    {
      $demo_id_all= $fetch_demo_id['demo_ID'];
      $sql="INSERT INTO `webtech`.`order_game` (`order_game_ID`, `game_ID`, `game_price`, `order_ID`) VALUES (NULL, '$demo_id_all', '$demo_price', '$order_id')";
	  mysql_query($sql);
    }

    $sub =$demo_price *  $num_demo_id;

    echo "<tr align='center'>";
    echo "<td class='tableContent'> $demo_id </td>";
    echo "<td class='tableContent'> &nbsp; $demo_name </td>";
    echo "<td class='tableContent'> $num_demo_id  </td>";
    echo "<td class='tableContent'> x </td>";
    echo "<td class='tableContent'> $ $demo_price </td>";
    echo "<td class='tableContent'> = </td>";
    echo "<td class='tableContent'> $ $sub </td>";
    echo "</tr>";
	
  }
  if (!$displayFromDB) {
	  echo "<tr>";
	  echo "<td colspan='6' align='right' style='padding:3; background-color:#CCC'> <strong> Total Price &nbsp; </strong> </td>";
	  echo "<td align='center' style='padding:3; background-color:#CCC'> $ $total </td>";
	  echo "</tr>";
  }
	if($displayFromDB) {
		$sql = mysql_query("SELECT * FROM `order` WHERE user_ID='$login_session' AND order_status='Pending'");
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$rowid = $row['order_ID'];
			$idsql = mysql_query("SELECT DISTINCT game_ID, game_price FROM order_game WHERE order_ID='$rowid'");
			while ($gamerow = mysql_fetch_array($idsql, MYSQL_ASSOC)) {
				echo "<tr align='center'>";
				echo "<td class='tableContent'>" . $gamerow['game_ID'] . "</td>";
				$gameid = $gamerow['game_ID'];
				$name = mysql_query("SELECT demo_name FROM demo WHERE demo_ID='$gameid'");
				$name = mysql_fetch_assoc($name);
				echo "<td class='tableContent'>" . $name['demo_name'] . "</td>";
				$quantity=mysql_query("SELECT * FROM order_game WHERE order_ID='$rowid' AND game_ID='$gameid'");
				$quantity = mysql_num_rows($quantity);
				echo "<td class='tableContent'>" . $quantity . "</td>";
				echo "<td class='tableContent'> x </td>";
				echo "<td class='tableContent'>" . $gamerow['game_price'] . "</td>";
				echo "<td class='tableContent'> = </td>";
				$unitprice = $gamerow['game_price'];
				$totalprice = floatval($quantity) * floatval($unitprice);
				$total += $totalprice;
				echo "<td class='tableContent'>" . $totalprice . "</td>";
				echo "</tr>";
			}
		}
	  echo "<tr>";
	  echo "<td colspan='6' align='right' style='padding:3; background-color:#CCC'> <strong> Total Price &nbsp; </strong> </td>";
	  echo "<td align='center' style='padding:3; background-color:#CCC'> $ $total </td>";
	  echo "</tr>";
	}
	
  $hour= date("H") + 6;
	if($hour > 23)
	{
	$hour = $hour - 24;
	}
	$order_date_final = date("Y-m-d $hour:i:s");
  
?>

</table>
<br />

<p align="center"> Check-Out Date: <?php echo $order_date_final?> </p>
<p align='center'><?php echo "Items Checked-out"?></p>


<?php
  //remove from cart
  $sql_delete="DELETE FROM cart WHERE user_ID='$login_session'";
  mysql_query($sql_delete);
  //-- end of remove   
?>    


</div>
<!-- InstanceEndEditable -->
</div>

<div id="footer">
<p align="center"> &copy; &nbsp; 2012 XGame </p>
</div>

</div>

</div>

</body>
<!-- InstanceEnd --></html>
